﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace YasharEl.Infrastructure
{
    public static class DateTimeExtensions
    {
        public const string Format = "yyyy-MM-dd HH:mm:ss:ffffff Z";

        /// <summary>
        /// Converts the date time to a string suitable for transport over the wire
        /// </summary>
        /// <returns></returns>
        public static string ToWireFormattedString(this DateTime time)
        {
            return time.ToUniversalTime().ToString(Format, CultureInfo.InvariantCulture);
        }

        /// <summary>
        /// Converts the date time to a string suitable for transport over the wire
        /// </summary>
        /// <returns></returns>
        public static DateTime ToUtcDateTime(this string time)
        {
            return DateTime.ParseExact(time, Format, CultureInfo.InvariantCulture).ToUniversalTime();
        }

        public static DateTime TruncateToSeconds(this DateTime source)
        {
            return new DateTime(source.Ticks - (source.Ticks % TimeSpan.TicksPerSecond), source.Kind);
        }
    }
}
